package com.ruoyi.inducePest.controller.InducePest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.inducePest.domain.Rect;
import com.ruoyi.inducePest.domain.inducePest.*;
import com.ruoyi.inducePest.service.inducePest.InducePestDataAnalysisService;
import com.ruoyi.utils.MyDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author LiSuxuan
 * @date 2021年 08月 31日 15:29
 */
@Api("数据分析接口")
@Slf4j
@RestController
@RequestMapping("/inducePest/dataAnalysis")
public class InducePestDataAnalysisController extends BaseController {

    @Autowired
    private InducePestDataAnalysisService inducePestDataAnalysisService;

    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/difTime")
    public AjaxResult insectDiffTime(String startTime, String endTime,String imgPest) throws ParseException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        Integer userId=null;
        //}
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        /* 存放结果数据集 */
        List<InducePestBroadcastInfo> statistics = new ArrayList<>();
        //是否是只取一天中的最后一张照片
        boolean isFirst=true;
        //记录前一天的数量
        InducePestNumType yesterdayNumType = new InducePestNumType();
        List<String> insectYesList= inducePestDataAnalysisService.selectLastInsectByDay(simpleDateFormat.parse(dates.get(0)),imgPest,userId);
        yesterdayNumType=json2InsectType(insectYesList,isFirst);

        InducePestNumType accumulationNumType = new InducePestNumType();

        for (String date : dates) {


            Date dayOfStart= simpleDateFormat.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dayOfStart);

            c.add(Calendar.DAY_OF_MONTH, 1);
            Date dayOfend = c.getTime();//这是明天

            //今日图片里所有的害虫数量
            InducePestNumType insectType = new InducePestNumType();
            String corner="";

            List<InducePestImgInfo> inducePestImgInfos = inducePestDataAnalysisService.selectAllInfoByDay(dayOfStart, dayOfend,imgPest,userId);

            InducePestImgInfo maxInducePestImgInfo = new InducePestImgInfo();

            if(inducePestImgInfos.size()==0){
                insectType=yesterdayNumType;
            }else {
                Integer maxInducePestNumber=-1;
                InducePestNumType maxInsectType = new InducePestNumType();
                for (InducePestImgInfo inducePestImgInfo : inducePestImgInfos) {
                    InducePestNumType tmpInsectType = new InducePestNumType();
                    List<String> tagManualList=new ArrayList<String>();
                    //今日图片里所有的害虫数量
                    if(inducePestImgInfo.getTagManual()!=null ){

                        tagManualList.add(inducePestImgInfo.getTagManual());
                        tmpInsectType=json2InsectType(tagManualList,isFirst);
                    }else {
                        tagManualList.add(inducePestImgInfo.getTag());
                        tmpInsectType = json2InsectType(tagManualList, isFirst);
                    }
                    if(getAllNumber(tmpInsectType)>maxInducePestNumber){
                        maxInducePestImgInfo=inducePestImgInfo;
                        maxInducePestNumber=getAllNumber(tmpInsectType);
                        maxInsectType=tmpInsectType;
                    }
                }
                insectType=maxInsectType;
            }


            //比较今天和昨天的数量后更新的数量
            InducePestNumType newDayChangeNumType = new InducePestNumType();

            InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();
            insectStatistic.setInducePestNumType(changePestsToImgPest(insectType,imgPest));
            imgPest= maxInducePestImgInfo.getImgPest();
            if(inducePestImgInfos.size()==0) {
                newDayChangeNumType.setCdtye(0);
                newDayChangeNumType.setEhm(0);
                newDayChangeNumType.setDm(0);
                newDayChangeNumType.setDzjym(0);
                newDayChangeNumType.setNc(0);

            }else if(maxInducePestImgInfo.getIsNewBoard()==1){
                if(imgPest!=null && imgPest.equals("草地贪夜蛾")){
                    newDayChangeNumType.setCdtye(insectType.getCdtye());
                }else {
                    newDayChangeNumType.setCdtye(0);
                }
                if(imgPest!=null && imgPest.equals("二化螟")){
                    newDayChangeNumType.setEhm(insectType.getEhm());
                }else {
                    newDayChangeNumType.setEhm(0);
                }
                if(imgPest!=null && imgPest.equals("大螟")){
                    newDayChangeNumType.setDm(insectType.getDm());
                }else {
                    newDayChangeNumType.setDm(0);
                }
                if(imgPest!=null && imgPest.equals("稻纵卷叶螟")){
                    newDayChangeNumType.setDzjym(insectType.getDzjym());
                }else {
                    newDayChangeNumType.setDzjym(0);
                }
                newDayChangeNumType.setNc(insectType.getNc());
            }else{
                if(insectType.getEhm()>=yesterdayNumType.getEhm() && imgPest!=null && imgPest.equals("二化螟")){
                    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                }else {
                    newDayChangeNumType.setEhm(0);
                }
                if(insectType.getCdtye()>=yesterdayNumType.getCdtye() && imgPest!=null &&  imgPest.equals("草地贪夜蛾")){
                    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                }else {
                    newDayChangeNumType.setCdtye(0);
                }
                if(insectType.getDzjym()>=yesterdayNumType.getDzjym() && imgPest!=null && imgPest.equals("稻纵卷叶螟")){
                    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                }else {
                    newDayChangeNumType.setDzjym(0);
                }
                if(insectType.getDm()>=yesterdayNumType.getDm() && imgPest!=null && imgPest.equals("大螟")){
                    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());

                }else {
                    newDayChangeNumType.setDm(0);
                }
                if(insectType.getNc()<yesterdayNumType.getNc()){
                    newDayChangeNumType.setNc(insectType.getNc() - yesterdayNumType.getNc());
                }else {
                    newDayChangeNumType.setNc(0);
                }
            }

            yesterdayNumType=insectType;

            accumulationNumType = addInducePestNumType(accumulationNumType, newDayChangeNumType);
            insectStatistic.setAccumulationNumType(changePestsToImgPest(accumulationNumType,imgPest));
            //临时，后面再改回图片中的虫子数
            insectStatistic.setInducePestNumType(changePestsToImgPest(accumulationNumType,imgPest));
            insectStatistic.setNewDayChangeNumType(changePestsToImgPest(newDayChangeNumType,imgPest));
            insectStatistic.setImgNum(inducePestDataAnalysisService.selectInsectImgNum(dayOfStart,dayOfend,imgPest,userId,null));
            insectStatistic.setDay(date);
            insectStatistic.setInsectTotalNumber(insectType.getEhm()+insectType.getDzjym()+insectType.getDm()+insectType.getCdtye());
            int average = 0;
            if (insectStatistic.getImgNum() > 0) {
                average = insectStatistic.getInsectTotalNumber() / insectStatistic.getImgNum() * 100;
            }
            insectStatistic.setAverageInsectTotalNumber(average);
            insectStatistic.setLevel(judgmentLevel(average));


            statistics.add(insectStatistic);
        }
        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/diffTimeByDeviceId")
    public AjaxResult insectDiffTimeByDeviceId(String startTime, String endTime,Integer deviceId,String imgPest) throws ParseException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        Integer userId=null;
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        /* 存放结果数据集 */
        List<InducePestBroadcastInfo> statistics = new ArrayList<>();
        //是否是只取一天中的第一张照片
        boolean isFirst=true;
        //记录前一天的数量
        InducePestNumType yesterdayNumType = new InducePestNumType();
        List<String> insectYesList= inducePestDataAnalysisService.selectLastInsectByDayAndDeviceId(simpleDateFormat.parse(dates.get(0)),deviceId,imgPest,userId);
        yesterdayNumType=json2InsectType(insectYesList,isFirst);

        InducePestNumType accumulationNumType = new InducePestNumType();

        for (String date : dates) {


            Date dayOfStart= simpleDateFormat.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dayOfStart);

            c.add(Calendar.DAY_OF_MONTH, 1);
            Date dayOfend = c.getTime();//这是明天

            //今日图片里所有的害虫数量
            InducePestNumType insectType = new InducePestNumType();
            String corner="";

            List<InducePestImgInfo> inducePestImgInfos = inducePestDataAnalysisService.selectAllInfoByDayAndDevice(dayOfStart, dayOfend,deviceId,imgPest,userId);

            InducePestImgInfo maxInducePestImgInfo = new InducePestImgInfo();

            if(inducePestImgInfos.size()==0){
                insectType=yesterdayNumType;
            }else {
                Integer maxInducePestNumber=-1;
                InducePestNumType maxInsectType = new InducePestNumType();
                for (InducePestImgInfo inducePestImgInfo : inducePestImgInfos) {
                    InducePestNumType tmpInsectType = new InducePestNumType();
                    List<String> tagManualList=new ArrayList<String>();
                    //今日图片里所有的害虫数量
                    if(inducePestImgInfo.getTagManual()!=null ){

                        tagManualList.add(inducePestImgInfo.getTagManual());
                        tmpInsectType=json2InsectType(tagManualList,isFirst);
                    }else {
                        tagManualList.add(inducePestImgInfo.getTag());
                        tmpInsectType = json2InsectType(tagManualList, isFirst);
                    }
                    if(getAllNumber(tmpInsectType)>maxInducePestNumber){
                        maxInducePestImgInfo=inducePestImgInfo;
                        maxInducePestNumber=getAllNumber(tmpInsectType);
                        maxInsectType=tmpInsectType;
                    }
                }
                insectType=maxInsectType;
            }


            //比较今天和昨天的数量后更新的数量
            InducePestNumType newDayChangeNumType = new InducePestNumType();

            InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();
            insectStatistic.setInducePestNumType(changePestsToImgPest(insectType,imgPest));
            imgPest= maxInducePestImgInfo.getImgPest();

            if(inducePestImgInfos.size()==0) {
                newDayChangeNumType.setCdtye(0);
                newDayChangeNumType.setEhm(0);
                newDayChangeNumType.setDm(0);
                newDayChangeNumType.setDzjym(0);
                newDayChangeNumType.setNc(0);

            }else if(maxInducePestImgInfo.getIsNewBoard()==1){
                newDayChangeNumType.setCdtye(insectType.getCdtye());
                newDayChangeNumType.setEhm(insectType.getEhm());
                newDayChangeNumType.setDm(insectType.getDm());
                newDayChangeNumType.setDzjym(insectType.getDzjym());
                //if (imgPest != null && imgPest.equals("草地贪夜蛾")) {
                //    newDayChangeNumType.setCdtye(insectType.getCdtye());
                //} else {
                //    newDayChangeNumType.setCdtye(0);
                //}
                //if (imgPest != null && imgPest.equals("二化螟")) {
                //    newDayChangeNumType.setEhm(insectType.getEhm());
                //} else {
                //    newDayChangeNumType.setEhm(0);
                //}
                //if (imgPest != null && imgPest.equals("大螟")) {
                //    newDayChangeNumType.setDm(insectType.getDm());
                //} else {
                //    newDayChangeNumType.setDm(0);
                //}
                //if (imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                //    newDayChangeNumType.setDzjym(insectType.getDzjym());
                //} else {
                //    newDayChangeNumType.setDzjym(0);
                //}
                newDayChangeNumType.setNc(insectType.getNc());
            }else{
                if (insectType.getEhm() >= yesterdayNumType.getEhm() ) {
                    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                } else {
                    newDayChangeNumType.setEhm(0);
                }
                if (insectType.getCdtye() >= yesterdayNumType.getCdtye() ) {
                    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                } else {
                    newDayChangeNumType.setCdtye(0);
                }
                if (insectType.getDzjym() >= yesterdayNumType.getDzjym() ) {
                    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                } else {
                    newDayChangeNumType.setDzjym(0);
                }
                if (insectType.getDm() >= yesterdayNumType.getDm() ) {
                    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());

                } else {
                    newDayChangeNumType.setDm(0);
                }
                //if (insectType.getEhm() >= yesterdayNumType.getEhm() && imgPest != null && imgPest.equals("二化螟")) {
                //    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                //} else {
                //    newDayChangeNumType.setEhm(0);
                //}
                //if (insectType.getCdtye() >= yesterdayNumType.getCdtye() && imgPest != null && imgPest.equals("草地贪夜蛾")) {
                //    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                //} else {
                //    newDayChangeNumType.setCdtye(0);
                //}
                //if (insectType.getDzjym() >= yesterdayNumType.getDzjym() && imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                //    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                //} else {
                //    newDayChangeNumType.setDzjym(0);
                //}
                //if (insectType.getDm() >= yesterdayNumType.getDm() && imgPest != null && imgPest.equals("大螟")) {
                //    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());
                //
                //} else {
                //    newDayChangeNumType.setDm(0);
                //}
                if(insectType.getNc()<yesterdayNumType.getNc()){
                    newDayChangeNumType.setNc(insectType.getNc() - yesterdayNumType.getNc());
                }else {
                    newDayChangeNumType.setNc(0);
                }
            }

            yesterdayNumType=insectType;
            accumulationNumType = addInducePestNumType(accumulationNumType, newDayChangeNumType);

            insectStatistic.setInducePestNumType(changePestsToImgPest(insectType,imgPest));
            insectStatistic.setAccumulationNumType(changePestsToImgPest(accumulationNumType,imgPest));
            insectStatistic.setNewDayChangeNumType(changePestsToImgPest(newDayChangeNumType,imgPest));
            insectStatistic.setImgNum(inducePestDataAnalysisService.selectInsectImgNum(dayOfStart,dayOfend,imgPest, userId,deviceId));
            insectStatistic.setDay(date);
            insectStatistic.setInsectTotalNumber(insectType.getEhm()+insectType.getDzjym()+insectType.getDm()+insectType.getCdtye());
            int average = 0;
            if (insectStatistic.getImgNum() > 0) {
                average = insectStatistic.getInsectTotalNumber() / insectStatistic.getImgNum() * 100;
            }
            insectStatistic.setAverageInsectTotalNumber(average);
            insectStatistic.setLevel(judgmentLevel(average));


            statistics.add(insectStatistic);
        }

        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "播报不同时间发生量（多个设备）", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/diffTimeByDeviceIds")
    public AjaxResult diffTimeByDeviceIds(String startTime, String endTime, @RequestParam(value ="deviceIds",required = false) String deviceIdsString, String imgPest) throws ParseException, JsonProcessingException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        List<NewInducePestBroadcastInfo> statistics =inducePestDataAnalysisService.diffTimeByDeviceIds(dates,deviceIdsString,imgPest);

        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/diffTimeByDeviceIdsForSingle")
    public AjaxResult diffTimeByDeviceIdsForSingle(String startTime, String endTime, @RequestParam(value ="deviceIds",required = false) String deviceIdsString, String imgPest) throws ParseException, JsonProcessingException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        List<NewInducePestBroadcastInfoForSingleDevice> statistics =inducePestDataAnalysisService.diffTimeByDeviceIdsForSingle(dates,deviceIdsString,imgPest);

        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/diffTimeByDeviceIdForPest")
    public AjaxResult diffTimeByDeviceIdForPest(String startTime, String endTime, Integer deviceId, String imgPest) throws ParseException, JsonProcessingException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        List<NewInducePestBroadcastInfo> statistics =inducePestDataAnalysisService.diffTimeByDeviceIdForPest(dates,deviceId,imgPest);

        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "统计多个设备的虫口量总数", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/totalByDevice")
    public AjaxResult totalByDevice(String startTime, String endTime, @RequestParam(value ="deviceIds",required = false) String deviceIdsString, String imgPest) throws ParseException, JsonProcessingException {
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }

        Map<String, Integer> result=inducePestDataAnalysisService.totalByDevice(dates,deviceIdsString,imgPest);

        return AjaxResult.success(result);
    }


    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/diffTimeByDeviceIdsOld")
    public AjaxResult diffTimeByDeviceIdsOld(String startTime, String endTime, @RequestParam(value ="deviceIds",required = false) String deviceIdsString, String imgPest) throws ParseException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        Integer userId=null;
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        /* 存放结果数据集 */
        List<InducePestBroadcastInfo> statistics = new ArrayList<>();

        //生成设备列表
        ArrayList<Integer> deviceIds = new ArrayList<>();
        if(StringUtils.isNotBlank(deviceIdsString)) {
            if (deviceIdsString.contains(",")) {
                for (String s : deviceIdsString.split(",")) {
                    deviceIds.add(Integer.parseInt(s));
                }
            } else {
                deviceIds.add(Integer.parseInt(deviceIdsString));
            }
        }else {
            return insectDiffTime( startTime,  endTime,imgPest);
        }

        //是否是只取一天中的最后一张照片
        boolean isFirst=true;
        //记录前一天的数量

        Map yesterdayNumTypeMap=new HashMap<Integer,InducePestNumType>();
        for (Integer deviceId : deviceIds) {
            InducePestNumType yesterdayNumType;
            List<String> insectYesList= inducePestDataAnalysisService.selectLastInsectByDayAndDeviceId(simpleDateFormat.parse(dates.get(0)),deviceId,imgPest,userId);
            yesterdayNumType=json2InsectType(insectYesList,isFirst);
            yesterdayNumTypeMap.put(deviceId,yesterdayNumType);
        }




        InducePestNumType accumulationNumType = new InducePestNumType();

        //统计时间段内各类害虫的和
        InducePestNumType countNumTypeByTimeAndPest = new InducePestNumType();
        for (String date : dates) {

            InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();

            Date dayOfStart= simpleDateFormat.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dayOfStart);

            c.add(Calendar.DAY_OF_MONTH, 1);
            Date dayOfend = c.getTime();//这是明天

            //统计当日所有设备总和
            InducePestNumType allAccumulationNumType= new InducePestNumType();;
            InducePestNumType allNewDayChangeNumType = new InducePestNumType();
            InducePestNumType allInsectType = new InducePestNumType();
            for (Integer deviceId : deviceIds) {
                InducePestNumType yesterdayNumType = (InducePestNumType) yesterdayNumTypeMap.get(deviceId);
                //今日图片里所有的害虫数量
                InducePestNumType insectType = new InducePestNumType();
                String corner = "";

                List<InducePestImgInfo> inducePestImgInfos = inducePestDataAnalysisService.selectAllInfoByDayAndDevice(dayOfStart, dayOfend, deviceId, imgPest, userId);

                InducePestImgInfo maxInducePestImgInfo = new InducePestImgInfo();

                if (inducePestImgInfos.size() == 0) {
                    insectType = yesterdayNumType;
                } else {
                    Integer maxInducePestNumber = -1;
                    InducePestNumType maxInsectType = new InducePestNumType();
                    for (InducePestImgInfo inducePestImgInfo : inducePestImgInfos) {
                        InducePestNumType tmpInsectType = new InducePestNumType();
                        List<String> tagManualList = new ArrayList<String>();
                        //今日图片里所有的害虫数量
                        if (inducePestImgInfo.getTagManual() != null && inducePestImgInfo.getTagManual().length() != 0) {

                            tagManualList.add(inducePestImgInfo.getTagManual());
                            tmpInsectType = json2InsectType(tagManualList, isFirst);
                        } else {
                            tagManualList.add(inducePestImgInfo.getTag());
                            tmpInsectType = json2InsectType(tagManualList, isFirst);
                        }
                        if (getAllNumber(tmpInsectType) > maxInducePestNumber) {
                            maxInducePestImgInfo = inducePestImgInfo;
                            maxInducePestNumber = getAllNumber(tmpInsectType);
                            maxInsectType = tmpInsectType;
                        }
                    }
                    insectType = maxInsectType;
                }


                //比较今天和昨天的数量后更新的数量
                InducePestNumType newDayChangeNumType = new InducePestNumType();



                //imgPest = maxInducePestImgInfo.getImgPest();

                if (inducePestImgInfos.size() == 0) {
                    newDayChangeNumType.setCdtye(0);
                    newDayChangeNumType.setEhm(0);
                    newDayChangeNumType.setDm(0);
                    newDayChangeNumType.setDzjym(0);
                    newDayChangeNumType.setNc(0);

                } else if (maxInducePestImgInfo.getIsNewBoard() == 1) {
                    newDayChangeNumType.setCdtye(insectType.getCdtye());
                    newDayChangeNumType.setEhm(insectType.getEhm());
                    newDayChangeNumType.setDm(insectType.getDm());
                    newDayChangeNumType.setDzjym(insectType.getDzjym());
                    //if (imgPest != null && imgPest.equals("草地贪夜蛾")) {
                    //    newDayChangeNumType.setCdtye(insectType.getCdtye());
                    //} else {
                    //    newDayChangeNumType.setCdtye(0);
                    //}
                    //if (imgPest != null && imgPest.equals("二化螟")) {
                    //    newDayChangeNumType.setEhm(insectType.getEhm());
                    //} else {
                    //    newDayChangeNumType.setEhm(0);
                    //}
                    //if (imgPest != null && imgPest.equals("大螟")) {
                    //    newDayChangeNumType.setDm(insectType.getDm());
                    //} else {
                    //    newDayChangeNumType.setDm(0);
                    //}
                    //if (imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                    //    newDayChangeNumType.setDzjym(insectType.getDzjym());
                    //} else {
                    //    newDayChangeNumType.setDzjym(0);
                    //}
                    newDayChangeNumType.setNc(insectType.getNc());
                } else {
                    if (insectType.getEhm() >= yesterdayNumType.getEhm() ) {
                        newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                    } else {
                        newDayChangeNumType.setEhm(0);
                    }
                    if (insectType.getCdtye() >= yesterdayNumType.getCdtye() ) {
                        newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                    } else {
                        newDayChangeNumType.setCdtye(0);
                    }
                    if (insectType.getDzjym() >= yesterdayNumType.getDzjym() ) {
                        newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                    } else {
                        newDayChangeNumType.setDzjym(0);
                    }
                    if (insectType.getDm() >= yesterdayNumType.getDm() ) {
                        newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());

                    } else {
                        newDayChangeNumType.setDm(0);
                    }
                    //if (insectType.getEhm() >= yesterdayNumType.getEhm() && imgPest != null && imgPest.equals("二化螟")) {
                    //    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                    //} else {
                    //    newDayChangeNumType.setEhm(0);
                    //}
                    //if (insectType.getCdtye() >= yesterdayNumType.getCdtye() && imgPest != null && imgPest.equals("草地贪夜蛾")) {
                    //    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                    //} else {
                    //    newDayChangeNumType.setCdtye(0);
                    //}
                    //if (insectType.getDzjym() >= yesterdayNumType.getDzjym() && imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                    //    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                    //} else {
                    //    newDayChangeNumType.setDzjym(0);
                    //}
                    //if (insectType.getDm() >= yesterdayNumType.getDm() && imgPest != null && imgPest.equals("大螟")) {
                    //    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());
                    //
                    //} else {
                    //    newDayChangeNumType.setDm(0);
                    //}
                    if (insectType.getNc() < yesterdayNumType.getNc()) {
                        newDayChangeNumType.setNc(insectType.getNc() - yesterdayNumType.getNc());
                    } else {
                        newDayChangeNumType.setNc(0);
                    }
                }

                yesterdayNumTypeMap.put(deviceId,insectType);

                accumulationNumType = addInducePestNumType(accumulationNumType, newDayChangeNumType);
                allAccumulationNumType=addInducePestNumType(allAccumulationNumType,accumulationNumType);
                allNewDayChangeNumType=addInducePestNumType(allNewDayChangeNumType,newDayChangeNumType);
                allInsectType=addInducePestNumType(allInsectType,insectType);
            }
            insectStatistic.setInducePestNumType(changePestsToImgPest(allInsectType,imgPest));
            insectStatistic.setAccumulationNumType(changePestsToImgPest(allAccumulationNumType,imgPest));
            insectStatistic.setNewDayChangeNumType(changePestsToImgPest(allNewDayChangeNumType,imgPest));
            countNumTypeByTimeAndPest=addInducePestNumType(countNumTypeByTimeAndPest,allNewDayChangeNumType);
            insectStatistic.setImgNum(inducePestDataAnalysisService.selectInsectImgNum(dayOfStart,dayOfend,imgPest, userId,deviceIds.size()));
            insectStatistic.setDay(date);
            InducePestNumTypeNumber inducePestNumTypeNumber = changeInducePestNumTypeToInsectType(allNewDayChangeNumType);
            insectStatistic.setInsectType(changeNumberPestsToImgPest(inducePestNumTypeNumber,imgPest));
            insectStatistic.setInsectTotalNumber(allAccumulationNumType.getEhm()+allAccumulationNumType.getDzjym()+allAccumulationNumType.getDm()+allAccumulationNumType.getCdtye());
            int average = 0;
            if (insectStatistic.getImgNum() > 0) {
                average = insectStatistic.getInsectTotalNumber() / insectStatistic.getImgNum() * 100;
            }
            insectStatistic.setAverageInsectTotalNumber(average);
            insectStatistic.setLevel(judgmentLevel(average));


            statistics.add(insectStatistic);
        }
        InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();
        insectStatistic.setDay("ALL_DAY:CLASS_BY_SUM");
        InducePestNumTypeNumber inducePestNumTypeNumber = changeInducePestNumTypeToInsectType(countNumTypeByTimeAndPest);
        insectStatistic.setInsectType(changeNumberPestsToImgPest(inducePestNumTypeNumber,imgPest));
        statistics.add(insectStatistic);
        return AjaxResult.success(statistics);
    }

    @ApiOperation(value = "统计总数", notes = "示例:http://localhost:8181/tobaccoData/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/totalByDeviceOld")
    public AjaxResult totalByDeviceOld(String startTime, String endTime, @RequestParam(value ="deviceIds",required = false) String deviceIdsString, String imgPest) throws ParseException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        Integer userId=null;
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        /* 存放结果数据集 */
        List<InducePestBroadcastInfo> statistics = new ArrayList<>();

        //生成设备列表
        ArrayList<Integer> deviceIds = new ArrayList<>();
        if(StringUtils.isNotBlank(deviceIdsString)) {
            if (deviceIdsString.contains(",")) {
                for (String s : deviceIdsString.split(",")) {
                    deviceIds.add(Integer.parseInt(s));
                }
            } else {
                deviceIds.add(Integer.parseInt(deviceIdsString));
            }
        }else {
            return insectDiffTime( startTime,  endTime,imgPest);
        }

        //是否是只取一天中的第一张照片
        boolean isFirst=true;
        //记录前一天的数量

        Map yesterdayNumTypeMap=new HashMap<Integer,InducePestNumType>();
        for (Integer deviceId : deviceIds) {
            InducePestNumType yesterdayNumType;
            List<String> insectYesList= inducePestDataAnalysisService.selectLastInsectByDayAndDeviceId(simpleDateFormat.parse(dates.get(0)),deviceId,imgPest,userId);
            yesterdayNumType=json2InsectType(insectYesList,isFirst);
            yesterdayNumTypeMap.put(deviceId,yesterdayNumType);
        }




        InducePestNumType accumulationNumType = new InducePestNumType();

        //统计时间段内各类害虫的和
        InducePestNumType countNumTypeByTimeAndPest = new InducePestNumType();
        for (String date : dates) {

            InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();

            Date dayOfStart= simpleDateFormat.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dayOfStart);

            c.add(Calendar.DAY_OF_MONTH, 1);
            Date dayOfend = c.getTime();//这是明天

            //统计当日所有设备总和
            InducePestNumType allAccumulationNumType= new InducePestNumType();;
            InducePestNumType allNewDayChangeNumType = new InducePestNumType();
            for (Integer deviceId : deviceIds) {
                InducePestNumType yesterdayNumType = (InducePestNumType) yesterdayNumTypeMap.get(deviceId);
                //今日图片里所有的害虫数量
                InducePestNumType insectType = new InducePestNumType();
                String corner = "";

                List<InducePestImgInfo> inducePestImgInfos = inducePestDataAnalysisService.selectAllInfoByDayAndDevice(dayOfStart, dayOfend, deviceId, imgPest, userId);

                InducePestImgInfo maxInducePestImgInfo = new InducePestImgInfo();

                if (inducePestImgInfos.size() == 0) {
                    insectType = yesterdayNumType;
                } else {
                    Integer maxInducePestNumber = -1;
                    InducePestNumType maxInsectType = new InducePestNumType();
                    for (InducePestImgInfo inducePestImgInfo : inducePestImgInfos) {
                        InducePestNumType tmpInsectType = new InducePestNumType();
                        List<String> tagManualList = new ArrayList<String>();
                        //今日图片里所有的害虫数量
                        if (inducePestImgInfo.getTagManual() != null && inducePestImgInfo.getTagManual().length() != 0) {

                            tagManualList.add(inducePestImgInfo.getTagManual());
                            tmpInsectType = json2InsectType(tagManualList, isFirst);
                        } else {
                            tagManualList.add(inducePestImgInfo.getTag());
                            tmpInsectType = json2InsectType(tagManualList, isFirst);
                        }
                        if (getAllNumber(tmpInsectType) > maxInducePestNumber) {
                            maxInducePestImgInfo = inducePestImgInfo;
                            maxInducePestNumber = getAllNumber(tmpInsectType);
                            maxInsectType = tmpInsectType;
                        }
                    }
                    insectType = maxInsectType;
                }


                //比较今天和昨天的数量后更新的数量
                InducePestNumType newDayChangeNumType = new InducePestNumType();



                //imgPest = maxInducePestImgInfo.getImgPest();

                if (inducePestImgInfos.size() == 0) {
                    newDayChangeNumType.setCdtye(0);
                    newDayChangeNumType.setEhm(0);
                    newDayChangeNumType.setDm(0);
                    newDayChangeNumType.setDzjym(0);
                    newDayChangeNumType.setNc(0);

                } else if (maxInducePestImgInfo.getIsNewBoard() == 1) {
                    newDayChangeNumType.setCdtye(insectType.getCdtye());
                    newDayChangeNumType.setEhm(insectType.getEhm());
                    newDayChangeNumType.setDm(insectType.getDm());
                    newDayChangeNumType.setDzjym(insectType.getDzjym());
                    //if (imgPest != null && imgPest.equals("草地贪夜蛾")) {
                    //    newDayChangeNumType.setCdtye(insectType.getCdtye());
                    //} else {
                    //    newDayChangeNumType.setCdtye(0);
                    //}
                    //if (imgPest != null && imgPest.equals("二化螟")) {
                    //    newDayChangeNumType.setEhm(insectType.getEhm());
                    //} else {
                    //    newDayChangeNumType.setEhm(0);
                    //}
                    //if (imgPest != null && imgPest.equals("大螟")) {
                    //    newDayChangeNumType.setDm(insectType.getDm());
                    //} else {
                    //    newDayChangeNumType.setDm(0);
                    //}
                    //if (imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                    //    newDayChangeNumType.setDzjym(insectType.getDzjym());
                    //} else {
                    //    newDayChangeNumType.setDzjym(0);
                    //}
                    newDayChangeNumType.setNc(insectType.getNc());
                } else {
                    if (insectType.getEhm() >= yesterdayNumType.getEhm() ) {
                        newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                    } else {
                        newDayChangeNumType.setEhm(0);
                    }
                    if (insectType.getCdtye() >= yesterdayNumType.getCdtye() ) {
                        newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                    } else {
                        newDayChangeNumType.setCdtye(0);
                    }
                    if (insectType.getDzjym() >= yesterdayNumType.getDzjym() ) {
                        newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                    } else {
                        newDayChangeNumType.setDzjym(0);
                    }
                    if (insectType.getDm() >= yesterdayNumType.getDm() ) {
                        newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());

                    } else {
                        newDayChangeNumType.setDm(0);
                    }
                    //if (insectType.getEhm() >= yesterdayNumType.getEhm() && imgPest != null && imgPest.equals("二化螟")) {
                    //    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                    //} else {
                    //    newDayChangeNumType.setEhm(0);
                    //}
                    //if (insectType.getCdtye() >= yesterdayNumType.getCdtye() && imgPest != null && imgPest.equals("草地贪夜蛾")) {
                    //    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                    //} else {
                    //    newDayChangeNumType.setCdtye(0);
                    //}
                    //if (insectType.getDzjym() >= yesterdayNumType.getDzjym() && imgPest != null && imgPest.equals("稻纵卷叶螟")) {
                    //    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                    //} else {
                    //    newDayChangeNumType.setDzjym(0);
                    //}
                    //if (insectType.getDm() >= yesterdayNumType.getDm() && imgPest != null && imgPest.equals("大螟")) {
                    //    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());
                    //
                    //} else {
                    //    newDayChangeNumType.setDm(0);
                    //}
                    if (insectType.getNc() < yesterdayNumType.getNc()) {
                        newDayChangeNumType.setNc(insectType.getNc() - yesterdayNumType.getNc());
                    } else {
                        newDayChangeNumType.setNc(0);
                    }
                }

                yesterdayNumType = insectType;

                accumulationNumType = addInducePestNumType(accumulationNumType, newDayChangeNumType);
                allAccumulationNumType=addInducePestNumType(allAccumulationNumType,accumulationNumType);
                allNewDayChangeNumType=addInducePestNumType(allNewDayChangeNumType,newDayChangeNumType);
            }
            insectStatistic.setInducePestNumType(changePestsToImgPest(allAccumulationNumType,imgPest));
            insectStatistic.setAccumulationNumType(changePestsToImgPest(allAccumulationNumType,imgPest));
            insectStatistic.setNewDayChangeNumType(changePestsToImgPest(allNewDayChangeNumType,imgPest));
            countNumTypeByTimeAndPest=addInducePestNumType(countNumTypeByTimeAndPest,allNewDayChangeNumType);
            insectStatistic.setImgNum(inducePestDataAnalysisService.selectInsectImgNum(dayOfStart,dayOfend,imgPest, userId,deviceIds.size()));
            insectStatistic.setDay(date);
            InducePestNumTypeNumber inducePestNumTypeNumber = changeInducePestNumTypeToInsectType(allNewDayChangeNumType);
            insectStatistic.setInsectType(changeNumberPestsToImgPest(inducePestNumTypeNumber,imgPest));
            insectStatistic.setInsectTotalNumber(allAccumulationNumType.getEhm()+allAccumulationNumType.getDzjym()+allAccumulationNumType.getDm()+allAccumulationNumType.getCdtye());
            int average = 0;
            if (insectStatistic.getImgNum() > 0) {
                average = insectStatistic.getInsectTotalNumber() / insectStatistic.getImgNum() * 100;
            }
            insectStatistic.setAverageInsectTotalNumber(average);
            insectStatistic.setLevel(judgmentLevel(average));


            statistics.add(insectStatistic);
        }
        InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();
        insectStatistic.setDay("ALL_DAY:CLASS_BY_SUM");
        InducePestNumTypeNumber inducePestNumTypeNumber = changeInducePestNumTypeToInsectType(countNumTypeByTimeAndPest);
        insectStatistic.setInsectType(changeNumberPestsToImgPest(inducePestNumTypeNumber,imgPest));
        statistics.add(insectStatistic);
        HashMap<String, Integer> result = new HashMap<>();
        result.put("z2",inducePestNumTypeNumber.getZ2());
        result.put("z7",inducePestNumTypeNumber.getZ7());
        result.put("z15",inducePestNumTypeNumber.getZ15());

        return AjaxResult.success(result);
    }

    /**
     * 根据条件导出excel全部符合条件的调查信息（图片信息）
     * @return
     */
    @ApiOperation(value = "根据条件导出excel全部符合条件的调查信息（图片信息）", notes = "示例:http://192.168.1.217:8989/exportSelectInvestigateInfoByCondition\n请求头附带查询条件\n返回信息列表")
    //@ResponseBody
    @PostMapping("/exportExcelVue")
    public AjaxResult exportSelectInvestigateInfoByConditionVue(String startTime, String endTime, Integer deviceId,String imgPest,HttpServletResponse response, HttpServletRequest request) throws ParseException {
        //Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        //Integer userId=Integer.valueOf(userIdL.intValue());
        //if(userId==1){
        //    userId=null;
        //}
        Integer userId=null;
        ExcelUtil<ExportExcel> util = new ExcelUtil<ExportExcel>(ExportExcel.class);
        List<ExportExcel> returnExport = new ArrayList<>();
        AjaxResult excelresult = new AjaxResult();
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        /* 存放结果数据集 */
        //记录前一天的数量


        Map<Integer, InducePestNumType> record = new HashMap<>();

        boolean isFirst=true;
        InducePestNumType yesterdayNumType = new InducePestNumType();
        List<String> insectYesList= inducePestDataAnalysisService.selectLastInsectByDayAndDeviceId(simpleDateFormat.parse(dates.get(0)),deviceId,imgPest,userId);
        yesterdayNumType=json2InsectType(insectYesList,isFirst);
        for (String date : dates) {

            ExportExcel exportExcel = new ExportExcel();
            exportExcel.setDate(date);

            Date dayOfStart= simpleDateFormat.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dayOfStart);

            c.add(Calendar.DAY_OF_MONTH, 1);
            Date dayOfend = c.getTime();//这是明天

            //今日图片里所有的害虫数量
            InducePestNumType insectType = new InducePestNumType();
            String corner="";

            List<InducePestImgInfo> inducePestImgInfos = inducePestDataAnalysisService.selectAllInfoByDayAndDevice(dayOfStart, dayOfend,deviceId,imgPest,userId);

            InducePestImgInfo maxInducePestImgInfo = new InducePestImgInfo();

            if(inducePestImgInfos.size()==0){
                insectType=yesterdayNumType;
            }else {
                Integer maxInducePestNumber=-1;
                InducePestNumType maxInsectType = new InducePestNumType();
                for (InducePestImgInfo inducePestImgInfo : inducePestImgInfos) {
                    InducePestNumType tmpInsectType = new InducePestNumType();
                    List<String> tagManualList=new ArrayList<String>();
                    //今日图片里所有的害虫数量
                    if(inducePestImgInfo.getTagManual()!=null && inducePestImgInfo.getTagManual().length()!=0){

                        tagManualList.add(inducePestImgInfo.getTagManual());
                        tmpInsectType=json2InsectType(tagManualList,isFirst);
                    }else {
                        tagManualList.add(inducePestImgInfo.getTag());
                        tmpInsectType = json2InsectType(tagManualList, isFirst);
                    }
                    if(getAllNumber(tmpInsectType)>maxInducePestNumber){
                        maxInducePestImgInfo=inducePestImgInfo;
                        maxInducePestNumber=getAllNumber(tmpInsectType);
                        maxInsectType=tmpInsectType;
                    }
                }
                insectType=maxInsectType;
            }


            //比较今天和昨天的数量后更新的数量
            InducePestNumType newDayChangeNumType = new InducePestNumType();

            InducePestBroadcastInfo insectStatistic = new InducePestBroadcastInfo();
            insectStatistic.setInducePestNumType(changePestsToImgPest(insectType,imgPest));


            if(inducePestImgInfos.size()==0) {
                newDayChangeNumType.setCdtye(0);
                newDayChangeNumType.setEhm(0);
                newDayChangeNumType.setDm(0);
                newDayChangeNumType.setDzjym(0);
                newDayChangeNumType.setNc(0);

            }else if(maxInducePestImgInfo.getIsNewBoard()==1){

                newDayChangeNumType.setCdtye(insectType.getCdtye());
                newDayChangeNumType.setEhm(insectType.getEhm());
                newDayChangeNumType.setDm(insectType.getDm());
                newDayChangeNumType.setDzjym(insectType.getDzjym());
                newDayChangeNumType.setNc(insectType.getNc());
            }else{
                if(insectType.getEhm()<yesterdayNumType.getEhm()){
                    newDayChangeNumType.setEhm(0);
                }else {
                    newDayChangeNumType.setEhm(insectType.getEhm() - yesterdayNumType.getEhm());
                }
                if(insectType.getCdtye()<yesterdayNumType.getCdtye()){
                    newDayChangeNumType.setCdtye(0);
                }else {
                    newDayChangeNumType.setCdtye(insectType.getCdtye() - yesterdayNumType.getCdtye());
                }
                if(insectType.getDzjym()<yesterdayNumType.getDzjym()){
                    newDayChangeNumType.setDzjym(0);
                }else {
                    newDayChangeNumType.setDzjym(insectType.getDzjym() - yesterdayNumType.getDzjym());
                }
                if(insectType.getDm()<yesterdayNumType.getDm()){
                    newDayChangeNumType.setDm(0);
                }else {
                    newDayChangeNumType.setDm(insectType.getDm() - yesterdayNumType.getDm());
                }
                if(insectType.getNc()<yesterdayNumType.getNc()){
                    newDayChangeNumType.setNc(0);
                }else {
                    newDayChangeNumType.setNc(insectType.getNc() - yesterdayNumType.getNc());
                }
            }

            yesterdayNumType=insectType;

            record.put(deviceId,insectType);
            exportExcel.setEhm(newDayChangeNumType.getEhm());

            exportExcel.setDeviceId(deviceId);
            returnExport.add(exportExcel);

        }
        excelresult = util.exportExcel(returnExport, "性诱害虫监测新增数据表");
        return excelresult;
    }

    /**
     * 在图像由用户进行分类的情况下，将所有识别结果都作为imgPest
     * 就需要对原来的数据分析模块进行转换
     * @param inducePestNumType
     * @return
     */
    public InducePestNumType changePestsToImgPest(InducePestNumType inducePestNumType,String imgPest){
        if("草地贪夜蛾".equals(imgPest)){
            return new InducePestNumType(0,0,0,inducePestNumType.getSumCount(),0,0);
        }
        if("大螟".equals(imgPest)){
            return new InducePestNumType(inducePestNumType.getSumCount(),0,0,0,0,0);
        }
        if("稻纵卷叶螟".equals(imgPest)){
            return new InducePestNumType(0,0,inducePestNumType.getSumCount(),0,0,0);
        }
        if("玉米螟".equals(imgPest)){
            return new InducePestNumType(0,0,0,0,inducePestNumType.getSumCount(),0);
        }
        if("二化螟".equals(imgPest)){
            return new InducePestNumType(0,inducePestNumType.getSumCount(),0,0,0,0);
        }
        return inducePestNumType;

    }

    /**
     * 在图像由用户进行分类的情况下，将所有识别结果都作为imgPest
     * 就需要对原来的数据分析模块进行转换
     * @param inducePestNumTypeNumber
     * @return
     */
    public InducePestNumTypeNumber changeNumberPestsToImgPest(InducePestNumTypeNumber inducePestNumTypeNumber,String imgPest){
        if("草地贪夜蛾".equals(imgPest)){
            return new InducePestNumTypeNumber(0,0,inducePestNumTypeNumber.getSumCount(),0);
        }
        if("稻纵卷叶螟".equals(imgPest)){
            return new InducePestNumTypeNumber(0,inducePestNumTypeNumber.getSumCount(),0,0);
        }
        if("玉米螟".equals(imgPest)){
            return new InducePestNumTypeNumber(0,0,0,inducePestNumTypeNumber.getSumCount());
        }
        if("二化螟".equals(imgPest)){
            return new InducePestNumTypeNumber(inducePestNumTypeNumber.getSumCount(),0,0,0);
        }
        return inducePestNumTypeNumber;

    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @GetMapping("/deleteImageById")
    public AjaxResult deleteImageById(int id){
        Integer counts = inducePestDataAnalysisService.deleteInducePestImgInfo(id);
        if(counts==0){
            return AjaxResult.success("该信息在数据库中不存在，请刷新或联系管理员");
        }
        return AjaxResult.success("删除成功，请刷新");

    }

    /**
     * 两个InducePestNumType类型对象相加并返回
     * @return
     */
    public static InducePestNumType addInducePestNumType(InducePestNumType accumulationNumType,InducePestNumType newDayChangeNumType) {
        InducePestNumType returnInducePestNumType = new InducePestNumType();
        returnInducePestNumType.setEhm(accumulationNumType.getEhm()+newDayChangeNumType.getEhm());
        returnInducePestNumType.setCdtye(accumulationNumType.getCdtye()+newDayChangeNumType.getCdtye());
        returnInducePestNumType.setDm(accumulationNumType.getDm()+newDayChangeNumType.getDm());
        returnInducePestNumType.setDzjym(accumulationNumType.getDzjym()+newDayChangeNumType.getDzjym());
        returnInducePestNumType.setNc(accumulationNumType.getNc()+newDayChangeNumType.getNc());
        return returnInducePestNumType;
    }


    /**
     * 两个InducePestNumType类型对象相加并返回
     * @return
     */
    public static InducePestNumTypeNumber changeInducePestNumTypeToInsectType(InducePestNumType inducePestNumType) {
        InducePestNumTypeNumber returnInducePestNumTypeNumber = new InducePestNumTypeNumber();
        returnInducePestNumTypeNumber.setZ2(inducePestNumType.getDzjym());
        returnInducePestNumTypeNumber.setZ7(inducePestNumType.getEhm());
        returnInducePestNumTypeNumber.setZ15(inducePestNumType.getCdtye());

        return returnInducePestNumTypeNumber;
    }

    /**
     * 将数据库中的JSON格式转换为对象格式
     * Nc : 0,
     * 'dm': 1,
     * 'ehm': 2,
     * 'dzjym': 3,
     * 'cdtye': 4,
     *
     * @param list list列表
     * @return InsectType类型
     */
    public static InducePestNumType json2InsectType(List<String> list,boolean isFirst) {

        int dm = 0;

        int ehm = 0;

        int dzjym = 0;

        int cdtye = 0;

        int Nc = 0;

        int ymm = 0;

        for (String s : list) {
            if (s != null && s.length()!=0) {
                JSONObject totalNum = JSON.parseObject(s);
                if (totalNum.containsKey("1")) {
                    dm += (Integer) totalNum.get("1");
                }
                if (totalNum.containsKey("2")) {
                    ehm += (Integer) totalNum.get("2");
                }
                if (totalNum.containsKey("ehm")) {
                    ehm += (Integer) totalNum.get("ehm");
                }
                if (totalNum.containsKey("3")) {
                    dzjym += (Integer) totalNum.get("3");
                }
                if (totalNum.containsKey("dzjym")) {
                    dzjym += (Integer) totalNum.get("dzjym");
                }
                if (totalNum.containsKey("4")) {
                    cdtye += (Integer) totalNum.get("4");
                }
                if (totalNum.containsKey("cdtye")) {
                    cdtye += (Integer) totalNum.get("cdtye");
                }
                if (totalNum.containsKey("ymm")) {
                    ymm += (Integer) totalNum.get("ymm");
                }

                if (totalNum.containsKey("0")) {
                    Nc += (Integer) totalNum.get("0");
                }
                if(isFirst){
                    break;
                }
            }
        }
        return new InducePestNumType(dm, ehm, dzjym, cdtye,ymm, Nc);
    }

    public static Integer getAllNumber(InducePestNumType inducePestNumType){
        Integer result=0;
        if(inducePestNumType.getDm()!=null){
            result+=inducePestNumType.getDm();
        }
        if(inducePestNumType.getCdtye()!=null){
            result+=inducePestNumType.getCdtye();
        }
        if(inducePestNumType.getNc()!=null){
            result+=inducePestNumType.getNc();
        }
        if(inducePestNumType.getDzjym()!=null){
            result+=inducePestNumType.getDzjym();
        }
        if(inducePestNumType.getEhm()!=null){
            result+=inducePestNumType.getEhm();
        }
        if(inducePestNumType.getYmm()!=null){
            result+=inducePestNumType.getYmm();
        }
        return result;
    }


    /**
     * 判断预警等级 0是最低 4 最高
     *
     * @param quantity 数量
     * @return 等级
     */
    public static int judgmentLevel(Integer quantity) {
        if ((quantity < 1000) && (quantity > 0)) {
            return 1;
        }
        if ((quantity >= 1000) && (quantity < 3000)) {
            return 2;
        }
        if ((quantity >= 3000) && (quantity < 8000)) {
            return 3;
        }
        if (quantity >= 8000) {
            return 4;
        }
        return 0;
    }

    /**
     * 计算图片IOU
     *
     * @param
     * @return 等级
     */
    public static float iouCompute(Rect rect1, Rect rect2) {
        int leftColumnMax = Math.max(rect1.getX(), rect2.getX());
        int rightColumnMin = Math.min(rect1.getX()+rect1.getWidth(),rect2.getX()+rect2.getWidth());
        int upRowMax = Math.max(rect1.getY(), rect2.getY());
        int downRowMin = Math.min(rect1.getY()+rect1.getHeight(),rect2.getY()+ rect2.getHeight());

        if (leftColumnMax>=rightColumnMin || downRowMin<=upRowMax){
            return 0;
        }

        int s1 = rect1.getWidth()*rect1.getHeight();
        int s2 = rect2.getWidth()*rect2.getHeight();
        float sCross = (downRowMin-upRowMax)*(rightColumnMin-leftColumnMax);
        return sCross/(s1+s2-sCross);
    }

    /**
     * 计算图片IOU
     *
     * @param
     * @return 等级
     */
    public static boolean isNewBoard(String cornerYes,String corner) {
        String[] cornerYesArray=cornerYes.split(",");
        String[] cornerArray=corner.split(",");
        int sameBox=0;
        for (int i = 0; i < cornerYesArray.length; i=i+4) {
            Rect rectYes = new Rect();
            rectYes.setX(Integer.parseInt(cornerYesArray[i]));
            rectYes.setY(Integer.parseInt(cornerYesArray[i+1]));
            rectYes.setWidth(Integer.parseInt(cornerYesArray[i+2])-Integer.parseInt(cornerYesArray[i]));
            rectYes.setHeight(Integer.parseInt(cornerYesArray[i+3])-Integer.parseInt(cornerYesArray[i+1]));
            for (int j = 0; j < cornerArray.length; j=j+4) {
                Rect rect = new Rect();
                rect.setX(Integer.parseInt(cornerArray[j]));
                rect.setY(Integer.parseInt(cornerArray[j+1]));
                rect.setWidth(Integer.parseInt(cornerArray[j+2])-Integer.parseInt(cornerArray[j]));
                rect.setHeight(Integer.parseInt(cornerArray[j+3])-Integer.parseInt(cornerArray[j+1]));
                float result = iouCompute(rectYes, rect);
                if(result>=0.7){
                    sameBox++;
                    if(sameBox>=Math.floor(cornerYesArray.length/4/2)){
                        return true;
                    }
                    break;
                }
            }
        }
        return false;
    }
}
